package door.faultHandler;

import app.nbdevices.service.IFaultService;
import app.nbdevices.utils.ConvertUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mess.warningmess.Bean4WarnMessInfo;
import com.mess.warningmess.WarningMass;
import door.entity.DoorCycleReport;
import door.mapper.DoorCycleReportMapper;
import ele.entity.EleCycleReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import refactor.entity.Device;
import refactor.entity.Facilities;
import refactor.mapper.DeviceMapper;
import refactor.mapper.FacilitiesMapper;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

@Component
public class DoorFaultHandler {
    @Autowired
    DoorCycleReportMapper doorCycleReportMapper;

    @Autowired
    @Qualifier("FaultService")
    IFaultService svcFault;

    @Autowired
    DeviceMapper deviceMapper;

    @Autowired
    FacilitiesMapper facilitiesMapper;

    public  void mainHandler(DoorCycleReport doorCycleReport){
//
        ArrayList<Integer> faultTypeIds=new ArrayList<>();
//
        //停电检测
        if(doorCycleReport.getCityEle()==0){
            cityPowerFailure(doorCycleReport.getInternetDeviceId());
            faultTypeIds.add(15);
        }
        svcFault.updateEventLog(doorCycleReport.getInternetDeviceId(),faultTypeIds,"1");
    }

    public void dropLine(){
        Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        List<DoorCycleReport> res=doorCycleReportMapper.selectNewData();
        for(DoorCycleReport e:res){
//            if(second-e.getReportTime()>24*60*60*1000){

            if(second-e.getReportTime()>60*60*26){
                svcFault.saveEventLog(e.getInternetDeviceId(),7,12);
            }
        }
    }

    /**
     * 紧急断电
     * @param internetDeviceId
     */
    public void cityPowerFailure(String internetDeviceId){


        svcFault.saveEventLog(internetDeviceId,18,20);
    }

    /**
     * //短信通知，由于未找到责任人，暂未开通
     * @param internetDeviceId
     */
    public void messAlter(String internetDeviceId){
                Device device=null;
        List<Device> devs = deviceMapper.selectList(new QueryWrapper<Device>().eq("internet_device_id", internetDeviceId));
        if(devs.size()>0){
          device = devs.get(0);
            List<Facilities> facs = facilitiesMapper.selectList(new QueryWrapper<Facilities>().eq("id", device.getFacilitiesId()));
            if(facs!=null){
                Facilities facilities = facs.get(0);
                String[] res=facilities.getAllocationType().split("/");
                Bean4WarnMessInfo info = new Bean4WarnMessInfo(device.getCode(), facilities.getTaskType(), facilities.getDescription(), "", res[0], res[1]," ");
                		WarningMass.sendMess(info);

            }
        }
    }
}
